package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Pages;

public class GenericPagesDAO extends AutoKeys implements
		org.jetnuke.dao.PagesDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericPagesDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesModel#retrieveByPK(int)
	 */
	public Pages retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("PageModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Pages s = new Pages();

		if (rs.next()) {
			fillPagesFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillPagesFromResultSet(Pages s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("fillPagesFromResultSet(Pages, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setACTIVE(rs.getInt("ACTIVE"));
		s.setCID(rs.getInt("CID"));
		s.setCLANGUAGE(rs.getString("CLANGUAGE"));
		s.setCOUNTER(rs.getInt("COUNTER"));
		s.setDATE(rs.getTimestamp("DATE"));
		s.setPAGE_FOOTER(rs.getString("PAGE_FOOTER"));
		s.setPAGE_HEADER(rs.getString("PAGE_HEADER"));
		s.setPID(rs.getInt("PID"));
		s.setSIGNATURE(rs.getString("SIGNATURE"));
		s.setSUBTITLE(rs.getString("SUBTITLE"));
		s.setTEXT(rs.getString("TEXT"));
		s.setTITLE(rs.getString("TITLE"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillPagesFromResultSet(Pages, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Pages u = new Pages();
			fillPagesFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("PageModel.selectAll"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesModel#update(org.jetnuke.entities.Pages)
	 */
	public void update(Pages pg) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Pages) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("PageModel.update"));
		p.setInt(1, pg.getACTIVE());
		p.setInt(2, pg.getCID());
		p.setString(3, pg.getCLANGUAGE());
		p.setInt(4, pg.getCOUNTER());
		p.setTimestamp(5, new Timestamp(pg.getDATE().getTime()));
		p.setString(6, pg.getPAGE_FOOTER());
		p.setString(7, pg.getPAGE_HEADER());
		p.setString(8, pg.getSIGNATURE());
		p.setString(9, pg.getSUBTITLE());
		p.setString(10, pg.getTEXT());
		p.setString(11, pg.getTITLE());
		p.setInt(12, pg.getPID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Pages) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesModel#addNew(org.jetnuke.entities.Pages)
	 */
	public int addNew(Pages page) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Pages) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this.getStatementForAutoKeys("PageModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("PagesModel.lastGeneratedPageID"));
		initNewPage(page, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Pages) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param page
	 * @param p
	 */
	private void initNewPage(Pages pg, PreparedStatement p) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewPage(Pages, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, pg.getACTIVE());
		p.setInt(2, pg.getCID());
		p.setString(3, pg.getCLANGUAGE());
		p.setInt(4, pg.getCOUNTER());
		p.setTimestamp(5, new Timestamp(pg.getDATE().getTime()));
		p.setString(6, pg.getPAGE_FOOTER());
		p.setString(7, pg.getPAGE_HEADER());
		p.setString(8, pg.getSIGNATURE());
		p.setString(9, pg.getSUBTITLE());
		p.setString(10, pg.getTEXT());
		p.setString(11, pg.getTITLE());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewPage(Pages, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesModel#selectAllActive(int)
	 */
	public List selectAllActive(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActive(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PageModel.selectAllActive"));
		p.setInt(1, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActive(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesModel#selectAllActive()
	 */
	public List selectAllActive() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActive() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PageModel.selectAllActive1"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActive() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesModel#countActiveByCID(int)
	 */
	public int countActiveByCID(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countActiveByCID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PageModel.countActiveByCID"));
		p.setInt(1, cid);
		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countActiveByCID(int) - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.PagesModel#selectOrderCounterDesc(int)
	 */
	public List selectOrderCounterDesc(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderCounterDesc(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PageModel.selectOrderCounterDesc"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderCounterDesc(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

}
